<template>
    <div class="mainContent">
        <div class="box_left">
            <ul class="list_style">
                <li
                    :class="[activeIndex==item.value ? 'active' : '','li_style']"
                    v-for="(item, index) in deviceTypeList"
                    :key="index"
                    @click="clickDeviceTypeList(item.value)">
                    {{ item.desc }}
                </li>
            </ul>
        </div>
        <div class="box_right">
            <div class="search">
                <el-button type="primary" @click="isAddOrEdit(0)" icon="el-icon-plus" :disabled="!auth.isAdd">平台</el-button>
            </div>
            <div class="tableBox">
                <div class="tableList">
                    <el-table :data="tableList" height="6.3rem" :stripe="true">
                        <template slot="empty">
                            <div class="no_lsit" style="width:80%;margin:auto;">
                                <img src="../../assets/images/common/rotary_nodata.png" style="width:100%; height: 100%;" />
                            </div>
                        </template>
                        <el-table-column align="center" prop="platform" label="平台编号"></el-table-column>
                        <el-table-column align="center" prop="name" label="平台名称"></el-table-column>
                        <el-table-column align="center" prop="factory" label="公司名称"></el-table-column>
                        <el-table-column align="center" prop="remark" label="备注"></el-table-column>
                        <el-table-column align="center" prop="duration" label="操作">
                            <template slot-scope="scope">
                                <el-button
                                    @click.native.prevent="isAddOrEdit(1,scope.$index,tableList)"
                                    type="text"
                                    size="small"
                                    style="color:#00AFFB"
                                    :disabled="!auth.isEdit"
                                    >编辑
                                </el-button>
                                <el-button
                                    style="color:red"
                                    @click.native.prevent="remove(scope.$index,tableList)"
                                    type="text"
                                    size="small"
                                    :disabled="!auth.isDel"
                                    >删除
                                </el-button>
                            </template>
                        </el-table-column>
                    </el-table>
                </div>
                <div class="clearfix" v-show="tableList.length > 0">
                    <div class="float-right">
                        <Page
                            style="margin-top: 20px;text-align: center;"
                            :current.sync="current"
                            :total="totalCount"
                            :page-size="pageSize"
                            show-elevator
                            show-total
                            @on-change="pageChange"
                        ></Page>
                    </div>
                </div>
            </div>
        </div>

        <Modal v-model="modal" :title="isAdd ? '添加厂商' : '编辑厂商'" width="520px;" :mask-closable="false" class-name="msgModal">
            <div class="addBox">
                <div class="item">
                    <span class="tip">*</span>
                    <span>平台编号:</span>
                    <Input v-model="platform" placeholder="请输入" style="width: 300px" :disabled='!isAdd' clearable />
                </div>
                <div class="item">
                    <span class="tip">*</span>
                    <span>平台名称:</span>
                    <Input v-model="name" placeholder="请输入" style="width: 300px" :maxlength="32" clearable />
                </div>
                <div class="item">
                    <span>公司名称:</span>
                    <Input v-model="factory" placeholder="请输入" style="width: 300px" :maxlength="32" clearable />
                </div>
                <div class="item">
                    <span>备注信息:</span>
                    <Input v-model="remakes" placeholder="请输入" style="width: 300px;margin-right:40px" :maxlength="32" clearable />
                </div>
            </div>
            <div slot="footer">
                <el-button
                    @click="cancel(0)"
                    style="padding: 7px 25px;border-radius: 15px;"
                    type="info"
                    size="mini">
                    取消
                </el-button>
                <el-button
                    @click="dataSubmit"
                    type="primary"
                    style="padding: 7px 25px;border-radius: 15px;"
                    size="mini"
                    v-preventReClick="1500">
                    确定
                </el-button>
            </div>
        </Modal>

        <Modal
            v-model="delModal"
            title="删除厂商"
            width="360px;"
            :mask-closable="false"
            class-name="msgModal">
            <div style="margin: 10px">确定要删除此厂商吗？</div>
            <div slot="footer">
                <el-button
                    @click="cancel(1)"
                    style="padding: 7px 25px;border-radius: 15px;"
                    type="info"
                    size="mini">
                    取消
                </el-button>
                <el-button
                    @click="deleteHardwarePlatform"
                    type="primary"
                    style="padding: 7px 25px;border-radius: 15px;"
                    size="mini"
                    v-preventReClick="1500">
                    确定
                </el-button>
            </div>
        </Modal>
    </div>
</template>

<script>
import {
    getHardwarePlatform,
    getHardwarePlatformList,
    getAddHardwarePlatform,
    getUpdateHardwarePlatform,
    getDeleteHardwarePlatform,
    getAllHardwarePlatform
} from "@/api/api.js";
export default {
    name: "",
    data() {
        return {
            deviceTypeList: [],//左侧列表
            tableList: [],//表格数据
            activeIndex:1,//列表高亮标志
            deviceType: -1,//默认设备类型为-1
            firstDeviceType:"",//左侧列表数据自动首次执行提供devicetype
            current: 1,//当前页码
            pageSize: 10,//页数
            pageNum: 1,//页码
            totalCount: 0,//表格数据总量
            platform: "", //弹窗平台编号
            name: "", //弹窗平台名称
            factory: "", //弹窗公司名称
            remakes: "", //弹窗备注
            deleteID:'', //当前被删除平台代码
            delModal: false,//删除弹窗
            modal: false,//添加或编辑弹窗
            isAdd: false,//判断是否是添加或编辑标志
            auth:{
                isAdd: false,
                isEdit: false,
                isDel: false
            },  //添加，编辑或删除权限标志
            btnList:[] //权限列表
        };
    },
    watch:{
          deviceTypeList(){
              if(this.deviceTypeList){
                   this.firstDeviceType = this.deviceTypeList[0].value
                   this.getHardwarePlat(this.firstDeviceType)
              }
          }
    },
    methods: {
        /**
         * @description: 初始化权限操作
         */        
        initAuth(){
            this.auth.isAdd = this.btnList.includes('hdc/platform/add')
            this.auth.isEdit = this.btnList.includes('hdc/platform/edit')
            this.auth.isDel = this.btnList.includes('hdc/platform/delete')
        },

        /**
         * @description: 页码改变时触发事件
         * @param {*} index
         */        
        pageChange(index) {
            this.pageNum = index;
            this.getHardwarePlatList();
        },

        /**
         * @description: 左侧列表点击事件
         * @param {*} type  传递过来deviceType(硬件类型)
         */        
        clickDeviceTypeList(type) {
            this.activeIndex = type
            this.deviceType = type
            this.getHardwarePlat(type);

        },

        /**
         * @description: 添加或者编辑弹窗显示以及初始化
         * @param {*} type
         * @param {*} index
         * @param {*} rows
         */        
        isAddOrEdit(type, index, rows) {
            if (type == 0) {
                this.isAdd = true;
                this.modal = true;
                this.remakes = "";
                this.platform = "";
                this.name = "";
                this.factory = "";
                this.remakes = "";
            }
            if (type == 1) {
                this.isAdd = false;
                this.modal = true;
                this.platform = rows[index].platform;
                this.name = rows[index].name;
                this.factory = rows[index].factory;
                this.remakes = rows[index].remark;
            }
        },

        /**
         * @description: 弹窗隐藏方法
         * @param {*} type
         */        
        cancel(type) {
            if (type == 0) {
                this.modal = false;
            }
            if (type == 1) {
                this.delModal = false;
            }
        },

        remove(index, rows) {
            this.deleteID = rows[index].platform;
            this.delModal = true;
        },

        /**
         * @description: 添加或者编辑提交方法
         */        
        dataSubmit() {
            if (!this.platform) {
                this.$Modal.warning({
                    title: "您有必填字段",
                    content: "请输入平台编号"
                });
                return;
            }
            if (!this.name) {
                this.$Modal.warning({
                    title: "您有必填字段",
                    content: "请输入平台名称"
                });
                return;
            }
            
            if (this.isAdd) {
                this.addHardwarePlatform()
            } else {
                this.updateHardwarePlatform()
            }
        },

        /**
         * @description: 查询所有平台硬件
         */
        getAllHardwarePlat() {
            getAllHardwarePlatform()
                .then(res => {
                    if (res.code == 200) {
                        this.deviceTypeList = res.data.list || [];
                    } else {
                        this.$Message.warning(res.msg);
                    }
                })
                .catch(error => {
                    console.log(error);
                });
        },

        /**
         * @description:获取硬件平台
         * @param type
         */
        getHardwarePlat(type) {
            let params = {
                deviceType: type
            };
            getHardwarePlatform(params)
                .then(res => {
                    if (res.code == 200) {
                        let list = res.data.list || [];
                        this.tableList = list;
                        this.totalCount = this.tableList.length;
                    }
                })
                .catch(error => {
                    console.log(error);
                });
        },

        /**
         * @description: 获取硬件平台表格数据
         */
        getHardwarePlatList() {
            if(this.deviceType === -1){
                this.deviceType = this.firstDeviceType
            }
            let params = {
                deviceType: this.deviceType,
                pageNum: this.pageNum,
                pageSize: this.pageSize
            };
            getHardwarePlatformList(params)
                .then(res => {
                    if (res.code == 200) {
                        let list = res.data.list || [];
                        this.tableList = list;
                        this.totalCount = res.data.total;
                    } else {
                        this.$Message.warning(res.msg);
                    }
                })
                .catch(error => {
                    console.log(error);
                });
        },

        /**
         * @description: 添加硬件平台
         */
        addHardwarePlatform() {
            if(this.deviceType === -1){
                this.deviceType = this.firstDeviceType
            }
            let params = {
                deviceType: this.deviceType,
                platform: this.platform,
                name: this.name,
                factory: this.factory,
                remark: this.remakes
            };
            getAddHardwarePlatform(params)
                .then(res => {
                    if (res.code == 200) {
                        this.$message({
                            message: "添加成功",
                            type: "success"
                        });
                        this.modal = false;
                        this.getHardwarePlatList()
                    }
                })
                .catch(error => {
                    console.log(error);
                });
        },

        /**
         * @description: 编辑硬件平台
         */
        updateHardwarePlatform() {
            if(this.deviceType === -1){
                this.deviceType = this.firstDeviceType
            }
            let params = {
                deviceType: this.deviceType,
                platform: this.platform,
                name: this.name,
                factory: this.factory,
                remark: this.remakes
            };
            getUpdateHardwarePlatform(params)
                .then(res => {
                    if (res.code == 200) {
                        this.$message({
                                message: '修改成功',
                                type: 'success'
                            });
                        this.modal = false
                        this.getHardwarePlatList()
                    }
                })
                .catch(error => {
                    console.log(error);
                });
        },

        /**
         * @description: 删除硬件平台
         */
        deleteHardwarePlatform() {
            if(this.deviceType === -1){
                this.deviceType = this.firstDeviceType
            }
            let params = {
                deviceType:this.deviceType,
                platform:this.deleteID
            };
            getDeleteHardwarePlatform(params)
                .then(res => {
                    if (res.code == 200) {
                        this.$message({
                        message: '删除成功',
                        type: 'success'
                    });
                     // 为了在删除n页的最后一条数据时能成功跳转回上一页
                    const totalPage = Math.ceil((this.totalCount - 1) / this.pageSize) // 总页数
                    this.pageNum = this.pageNum > totalPage ? totalPage : this.pageNum //当前页码判断
                    this.pageNum = this.pageNum < 1 ? 1 : this.pageNum //当前页码判断
                    this.delModal = false
                    this.getHardwarePlatList()
                }else {
                    this.$Message.warning(res.msg);
                }
                })
                .catch(error => {
                    console.log(error);
                });
        }
    },
    created() {
        this.getAllHardwarePlat();
    },
    mounted() {
        this.btnList = JSON.parse(localStorage.getItem("arr"));//localStorage当中的arr项存取的是按钮权限列表
        this.initAuth() //初始化权限
    }
};
</script>

<style scoped lang="less">
@import "./../../assets/style/themeBase.less";
@import "./../../assets/style/mach.less";
@import "./../../assets/style/modal.less";
</style>

<style scoped lang="scss">
@import "./../../assets/style/evn/msgModal.scss";
.addBox {
    .item {
        span:first-child {
            margin-right: 20px;
        }
    }
}
/deep/.mainContent {
    width: 100%;
    height: 100%;
    padding: 10px;
    box-sizing: border-box;
    background: #f5f5f5;
    display: flex;
    .box_left {
        width: 375px;
        height: 100%;
        margin-right: 10px;
        padding: 20px;
        box-sizing: border-box;
        background-color: #ffffff;
        .list_style {
            width: 100%;
            height: 100%;
            overflow-y: auto;
            &::-webkit-scrollbar {
                width: 6px;
            }
            /*滚动槽*/
            &::-webkit-scrollbar-track {
                -webkit-box-shadow: inset 0 0 6px rgba(0, 0, 0, 0.1);
                box-shadow: inset 0 0 6px rgba(0, 0, 0, 0.1);
                border-radius: 10px;
            }
            /* 滚动条滑块 */
            &::-webkit-scrollbar-thumb {
                border-radius: 10px;
                background: rgba(0, 0, 0, 0.1);
                -webkit-box-shadow: inset 0 0 6px rgba(0, 0, 0, 0.1);
                box-shadow: inset 0 0 6px rgba(0, 0, 0, 0.1);
            }
            .li_style {
                display: block;
                list-style: none;
                border: 1px solid transparent;
                line-height: 2;
                padding: 5px 20px;
                background: white;
                cursor: pointer;
                &.active{
                    color: #fff;
                    z-index: -999;
                    background-color: var(--themeColor) !important;
                }
                &:nth-of-type(2n){
                    background-color: #F3F6FD;
                }
            }
    
        }
    }
    .box_right {
        flex: 1;
        height: 100%;
        .el-table__empty-text {
            width: 100% !important;
        }
        .search {
            padding: 20px;
            background-color: #fff;
        }
        .tableBox {
            width: 100%;
            height: calc(100% - 90px);
            margin-top: 10px;
            padding: 20px;
            background-color: #fff;
            box-sizing: border-box;
            canvas {
                display: none !important;
            }
            .tableList {
                width: 100%;
                margin: auto;
                .el-table {
                    color: #181818;
                    font-family: MicrosoftYaHei;
                    font-size: 14px;
                    td,
                    th.is-leaf {
                        border-bottom: none;
                    }
                    tr {
                        border: none;
                    }
                    thead th {
                        font-weight: 400;
                    }
                }
                .el-table__row {
                    background-color: #fff;
                    cursor: pointer;
                }
                .el-table--striped
                    .el-table__body
                    tr.el-table__row--striped
                    td {
                    background-color: #f3f6fd;
                    cursor: pointer;
                }
            }
        }
    }
}
/deep/ .addModal {
    .list {
        .item {
            position: relative;
            display: flex;
            justify-content: flex-start;
            align-items: center;
            margin-bottom: 15px;
            .span {
                width: 80px;
            }
            .tip {
                position: absolute;
                left: -5px;
                top: -5px;
                color: red;
            }
            .u_cord {
                flex: 1;
                height: 200px;
                border: 1px solid #eee;
                display: -webkit-box;
                display: -ms-flexbox;
                display: flex;
                -webkit-box-pack: center;
                -ms-flex-pack: center;
                justify-content: center;
                -webkit-box-align: center;
                -ms-flex-align: center;
                align-items: center;
                position: relative;
                cursor: pointer;
            }
            .u_cord span {
                font-size: 16px;
            }
            .u_cord img {
                display: block;
                width: 100%;
                height: 100%;
            }
        }
    }
    .search {
        width: 100%;
        padding: 0 25px;
        box-sizing: border-box;
        display: flex;
        justify-content: flex-start;
        align-items: center;
        .list {
            display: flex;
            justify-content: flex-start;
            align-items: center;
        }
    }
    .tableBox {
        width: 100%;
        padding: 20px;
        margin: 0 auto;
        background-color: #fff;
        box-sizing: border-box;
        canvas {
            display: none !important;
        }
        .tableList {
            width: 100%;
            margin: auto;
            .el-table {
                color: #181818;
                font-family: MicrosoftYaHei;
                font-size: 14px;
                td,
                th.is-leaf {
                    border-bottom: none;
                }
                tr {
                    border: none;
                }
            }
            .el-table__row {
                background-color: #fff;
                cursor: pointer;
            }
            .el-table--striped .el-table__body tr.el-table__row--striped td {
                background-color: #f3f6fd;
                cursor: pointer;
            }
            .is-disabled {
                background-color: transparent !important;
            }
        }
    }
}
/deep/ .transfer {
    position: relative;
    width: 90%;
    margin: auto;
    .el-transfer {
        display: flex;
        justify-content: space-between;
        align-items: center;
        .transfer-footer {
            display: flex;
            justify-content: center;
            align-items: center;
            margin-top: 3px;
            .ivu-input {
                border-radius: 15px;
            }
        }
    }
}
</style>
